package GUI;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;

import Control.UFO;

import com.modestmaps.core.Point2f;
import com.modestmaps.geo.Location;

public class UfoHighLight {
	int x,y,width=100,height;
	boolean state=false;
	boolean highLightState=false;
	int index=0;
	Button next = new Button("Next");
	Button previous = new Button("Previous");
	
	ArrayList<Button> highLightButtons = new ArrayList<Button>();

	int length=0;
	String shortSum = "Short Sum: ";
	int hdt=200;
	int xd,yd,wdt;
	
	DecimalFormat df = new DecimalFormat("#.##");
	
	
	
	public void createButtons(){
		
		
		if(GV.mapClass.highLightToDraw.size()>0){
			UFO u =GV.mapClass.highLightToDraw.get(0);
			Point2f p = GV.mapClass.map.locationPoint(new Location(u.getCoordinates().x, u.getCoordinates().y));

			x = (int) p.x;
			y = (int) p.y;
			height = GV.mapClass.highLightToDraw.size()*25;
			
			}
		
		highLightButtons = new ArrayList<Button>();
		for(int i=0;i<GV.mapClass.highLightToDraw.size();i++){

			UFO u = GV.mapClass.highLightToDraw.get(i);
			Point2f p = GV.mapClass.map.locationPoint(new Location(u.getCoordinates().x, u.getCoordinates().y));
			Button b = new Button(GV.monthName[u.getDateOfSight().get(Calendar.MONTH)]+
					" "+u.getDateOfSight().get(Calendar.DAY_OF_MONTH)+
					" "+(2000+u.getDateOfSight().get(Calendar.YEAR)));
			highLightButtons.add(b);
		}
	}
	

	public void draw(){
		if(GV.mapClass.airportHighLightToDraw!=null &&(!state && !highLightState) &&GV.hoursPanel.showAirportsButton.state){
			Point2f p = GV.mapClass.map.locationPoint(new Location(GV.mapClass.airportHighLightToDraw.getCoordinates().x,
					GV.mapClass.airportHighLightToDraw.getCoordinates().y));
			GV.processing.strokeWeight(2);
			GV.processing.rectMode(GV.processing.CORNER);
			GV.processing.fill(GV.backGroundColor);
			GV.processing.stroke(80);
			GV.processing.rect(p.x,p.y,GV.processing.textWidth(GV.mapClass.airportHighLightToDraw.getName()+"Name: ")+20,25);
			GV.processing.fill(GV.textColor);
			GV.processing.textAlign(GV.processing.LEFT);
			GV.processing.text("Name: "+GV.mapClass.airportHighLightToDraw.getName(),p.x+10,p.y+17);

		}
		if(GV.mapClass.militaryBaseToDraw!=null &&(!state && !highLightState) &&GV.hoursPanel.showMilitaryBasesButton.state){
			Point2f p = GV.mapClass.map.locationPoint(new Location(GV.mapClass.militaryBaseToDraw.getCoordinates().x,
					GV.mapClass.militaryBaseToDraw.getCoordinates().y));
			GV.processing.strokeWeight(2);
			GV.processing.rectMode(GV.processing.CORNER);
			GV.processing.fill(GV.backGroundColor);
			GV.processing.stroke(80);
			GV.processing.rect(p.x,p.y,GV.processing.textWidth(GV.mapClass.militaryBaseToDraw.getName()+"Name: ")+20,25);
			GV.processing.fill(GV.textColor);
			GV.processing.textAlign(GV.processing.LEFT);
			GV.processing.text("Name: "+GV.mapClass.militaryBaseToDraw.getName(),p.x+10,p.y+17);

		}
		
		if(!state && !highLightState) return;
		
		if(state &&!highLightState){
		GV.processing.strokeWeight(2);
		GV.processing.rectMode(GV.processing.CORNER);
		GV.processing.fill(GV.backGroundColor);
		GV.processing.stroke(80);
		GV.processing.rect(x,y,width,height);
		
		for(int i=0;i<highLightButtons.size();i++){
			Button b = highLightButtons.get(i);
			
			if(b.state){
				state=false;
				index=i;
				highLightState=true;
				break;
			}	
			b.x = x;
			b.y = y + 25*i;
			b.draw();
		}
		}
		else if (highLightState){
			drawUfoHighLight();
		}
		
	}
	
	private void drawUfoHighLight() {
		
		
		UFO u = GV.mapClass.highLightToDraw.get(index);
		length=200;
		ArrayList<String> tokens = formatString(u.getSummary(),28);
		ArrayList<String> airportTokens = formatString(GV.data.getAirportDataById(u.getClosestAirportId()).getName(),13);
		ArrayList<String> militaryTokens = formatString(u.getClosestBaseName(),13);
		wdt= length+80;
		if(x>GV.width/2)
			xd = x-wdt;
		else xd = x;
		if(y>GV.height/2)
			yd = y -200;
		else yd=y;
		
		hdt = ((tokens.size()+airportTokens.size() + militaryTokens.size())*15+155);
		
		GV.processing.strokeWeight(2);
		GV.processing.rectMode(GV.processing.CORNER);
		GV.processing.fill(GV.backGroundColor);
		GV.processing.stroke(80);
		GV.processing.rect(xd,yd,wdt,hdt);
		GV.processing.fill(GV.textColor);
		GV.processing.textFont(GV.plotFont, 13);
		GV.processing.textAlign(GV.processing.LEFT);
		int line =0;
		
		previous.x=xd;
		previous.y=yd+line;
		if(previous.state){
			if (index>0) index--;
			previous.state=false;
		}
		if(index>0)
		previous.draw();
		next.x=xd+wdt-45;
		next.y=yd+line;
		if(next.state){
			if (index<GV.mapClass.highLightToDraw.size()-1) index++;
			next.state=false;
		}
		if(index<GV.mapClass.highLightToDraw.size()-1)
		next.draw();
		GV.processing.stroke(GV.LineColor);
		GV.processing.line(xd, yd+line+30, xd+wdt, yd+line+30);
		line+=45;
		
		GV.processing.text("City:   "+u.getCity()+"   State:   "+u.getState(),xd+10,yd+line);
		line+=15;
		GV.processing.text("Date of sight:   "+GV.monthName[u.getDateOfSight().get(Calendar.MONTH)]+
				" "+u.getDateOfSight().get(Calendar.DAY_OF_MONTH)+
				" "+(2000+u.getDateOfSight().get(Calendar.YEAR)),xd+10,yd+line);
		line+=15;
		GV.processing.text("Shape:   "+u.getShape().getName(),xd+10,yd+line);
		line+=15;
		GV.processing.text("Duration:   "+u.getDuration().toLowerCase(),xd+10,yd+line);
		line += 15;
		GV.processing.text("Closest Airport: "+airportTokens.get(0),xd+10,yd+line);
		for(int i=1;i<airportTokens.size();i++){
			line+=15;
			GV.processing.text(airportTokens.get(i),xd+10,yd+line);
		}
		line+=15;
		GV.processing.text("Distance: "+(df.format(u.distanceFromClosestAirport))+ " Km",xd+10,yd+line);
		line+=15;
		GV.processing.text("Closest Military Base: "+militaryTokens.get(0),xd+10,yd+line);
		for(int i=1;i<militaryTokens.size();i++){
			line+=15;
			GV.processing.text(militaryTokens.get(i),xd+10,yd+line);
		}
		line+=15;
		GV.processing.text("Distance: "+(df.format(u.distanceFromClosestMilitaryBase))+ " Km",xd+10,yd+line);
		line+=15;
		GV.processing.text("Closest Weather Station: "+(df.format(u.getDistanceFromClosestWeatherStation()))+ " Km",xd+10,yd+line);
		if(tokens.size()>0){
			line+=15;
		GV.processing.text(shortSum+tokens.get(0).toLowerCase(),xd+10,yd+line);
		for(int j=1;j<tokens.size();j++){
			line+=15;
		GV.processing.text(tokens.get(j).trim().toLowerCase(),xd+10+GV.processing.textWidth(shortSum),yd+line);
		}
		}
		
		
	}

	private ArrayList<String> formatString(String string,int len){
		ArrayList<String> tokens = new ArrayList<String>();
		
		int i=0;
		for(i=len;i<string.length();i+=len){
			int start = i-len;
			while(string.substring(i-1, i).matches("[a-zA-Z]")){if(i<string.length())i++; else break;}
			String sub;
			if(i>=string.length()){ sub =string.substring(start);}
			else{ sub =string.substring(start, i);}
			if(length<GV.processing.textWidth(shortSum+sub)){
				length = (int)GV.processing.textWidth(shortSum+sub);
			}
			tokens.add(sub);
		}
		i-=len;
		if(i<string.length()-1)
		tokens.add(string.substring(i));
		return tokens;
	}
	
	
	public boolean mouseOver(){
		if(!state && !highLightState) return false;
		if(state && GV.processing.mouseX > x && GV.processing.mouseX < x + width && 
	    		GV.processing.mouseY > y && GV.processing.mouseY < y + height) 	return true;
		if(highLightState && GV.processing.mouseX > xd && GV.processing.mouseX < xd + wdt && 
	    		GV.processing.mouseY > yd && GV.processing.mouseY < yd + hdt)return true;
		return false;
	}
	


	public void mouseClicked() {
		
	}
}
